package com.sghj.record.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sghj.config.handler.ResultCode;
import com.sghj.config.handler.exceptionHandler.MyException;
import com.sghj.construction.entity.ConstructionSitePlan;
import com.sghj.construction.mapper.ConstructionSitePlanMapper;
import com.sghj.construction.service.ConstructionSitePlanService;
import com.sghj.record.entity.RecordIncall;
import com.sghj.record.entity.dto.RecordIncallQuery;
import com.sghj.record.entity.dto.RecordOutcallQuery;
import com.sghj.record.entity.vo.RecordIncallRes;
import com.sghj.record.mapper.RecordIncallMapper;
import com.sghj.record.service.RecordIncallService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhongkai
 * @since 2024-07-15
 */
@Service
public class RecordIncallServiceImpl extends ServiceImpl<RecordIncallMapper, RecordIncall> implements RecordIncallService {

    @Resource
    private ConstructionSitePlanService constructionSitePlanService;

    @Resource
    private ConstructionSitePlanMapper constructionSitePlanMapper;

    @Override
    public IPage<RecordIncallRes> getRecordIncallPage(RecordIncallQuery query, Page<RecordIncallRes> page) {
        return baseMapper.getRecordIncallPage(page, query);
    }

    @Override
    public List<RecordIncallRes> getRecordIncallByDayPlanNumber(Integer dayPlanNumber) {
        return baseMapper.getRecordIncallByDayPlanNumber(dayPlanNumber);
    }

    @Override
    public List<RecordIncallRes> getRecordIncallList(RecordIncallQuery query) {
        if (query == null || query.getQueryStartTime() == null || query.getQueryEndTime() == null){
            throw new MyException(ResultCode.FAIL,"查询条件不足");
        }
        return baseMapper.getRecordIncallList(query);
    }

    @Override
    public void insertInCallRecord(String callNo, Integer planId, Integer selectKey, String phoneNumber, Date inTime) {
        /* 1.根据planId查询对应施工计划*/
        ConstructionSitePlan constructionSitePlan = constructionSitePlanService.getPlanByDayPlanNumber(planId);

        //如果存在相应可操作的计划，则进行后续操作
        if (constructionSitePlan != null){
            //查询是否存在相应通话记录
            int checkRes = baseMapper.checkHasRecord(callNo);
            if (checkRes == 0){

                //插入通话记录
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                RecordIncall recordIncall =  new RecordIncall();
                recordIncall.setDayPlanNumber(planId);
                recordIncall.setCallId(callNo);
                recordIncall.setInPhoneNumber(phoneNumber);
                recordIncall.setInTime(inTime);
                recordIncall.setCallResult(String.valueOf(selectKey));
                recordIncall.setIsDelete(0);
                recordIncall.setCreateTime(new Date());
                recordIncall.setUpdateTime(new Date());

                if (selectKey == 1 || selectKey == 2 || selectKey == 3){
                    baseMapper.insert(recordIncall);

                    //更新施工计划
                    constructionSitePlan.setCallBackStatus(selectKey);
                    constructionSitePlan.setCallBackPhone(phoneNumber);
                    constructionSitePlan.setCallBackTime(inTime);
                    constructionSitePlan.setUpdateTime(new Date());
                    constructionSitePlanMapper.updateById(constructionSitePlan);
                }
            }
        }
    }
}
